package com.juli.school.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.alibaba.druid.util.StringUtils;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import com.juli.school.dao.entity.TestScope;
import com.juli.school.dao.mapper.TestScopeMapper;
import com.juli.school.service.bo.TestScopeBo;
import com.juli.school.service.utils.BeanUtils;
import com.juli.school.utils.core.model.CommonResp;

@Service
public class TestScopeService {

	private static final Logger logger = LoggerFactory.getLogger(LegService.class);
	
	@Resource
	TestScopeMapper testScopeMapper;

	public CommonResp<Boolean> add(TestScopeBo testScopeBo) {
		CommonResp<Boolean> commonResp = new CommonResp<Boolean>();
		try {
			if (testScopeBo.getSnStamp() == null) {
				commonResp.fail("心理测试编号不可为空");
				return commonResp;
			}
			testScopeMapper.insert(testScopeBo);
			commonResp.success();
		} catch (Exception e) {
			logger.error("新增心理测试分数信息异常", e);
			commonResp.exception("新增心理测试分数信息异常");
		}
		return commonResp;
	}

	public CommonResp<Boolean> update(TestScopeBo testScopeBo) {
		CommonResp<Boolean> commonResp = new CommonResp<Boolean>();
		try {
			if(Strings.isNullOrEmpty(testScopeBo.getSnStamp())) {
				commonResp.fail("心理测试编号不可为空");
				return commonResp;
			}
			testScopeMapper.update(testScopeBo);
			commonResp.success();
		} catch (Exception e) {
			logger.error("更新心理测试分数信息异常", e);
			commonResp.exception("更新心理测试分数信息异常");
		}
		return commonResp;
	}

	public CommonResp<Boolean> delete(String snStamp) {
		CommonResp<Boolean> commonResp = new CommonResp<Boolean>();
		try {
			if (StringUtils.isEmpty(snStamp)) {
				commonResp.fail("入参不能为空");
				return commonResp;
			}
			testScopeMapper.deleteBySnStamp(snStamp);
			commonResp.success();
		} catch (Exception e) {
			logger.error("删除心理测试分数信息异常", e);
			commonResp.exception("删除心理测试分数信息异常");
		}
		return commonResp;
	}

	public CommonResp<TestScope> detail(String snStamp, String num) {
		CommonResp<TestScope> commonResp = new CommonResp<TestScope>();
		try {
			if (Strings.isNullOrEmpty(snStamp) && Strings.isNullOrEmpty(num)) {
				commonResp.fail("入参不可为空");
				return commonResp;
			}
			TestScopeBo testScopeBo = new TestScopeBo();
			testScopeBo.setSnStamp(snStamp);
			testScopeBo.setNum(num);
			TestScope testScope = testScopeMapper.getTestScope(testScopeBo);
			if (testScope == null) {
				commonResp.exception("没有获取到信息");
				return commonResp;
			}
			commonResp.setData(testScope);
			commonResp.success();
		} catch (Exception e) {
			logger.error("获取发布信息详情发生异常", e);
			commonResp.exception("获取发布信息详情发生异常");
		}
		return commonResp;
	}

	public CommonResp<List<TestScopeBo>> listPage(String snStamp) {
		CommonResp<List<TestScopeBo>> commonResp = new CommonResp<List<TestScopeBo>>();
		try {
			if (Strings.isNullOrEmpty(snStamp)) {
				commonResp.fail("入参不能为空");
				return commonResp;
			}
			TestScopeBo req = new TestScopeBo();
			req.setSnStamp(snStamp);
			List<TestScope> listPage = testScopeMapper.listPage(req);
			List<TestScopeBo> data = Lists.newArrayList();
			for (TestScope testScopes : listPage) {
				TestScopeBo testScopeBo = new TestScopeBo();
				BeanUtils.copyProperties(testScopeBo, testScopes);
				data.add(testScopeBo);
			}
			commonResp.success();
			commonResp.setData(data);
		} catch (Exception e) {
			logger.error("获取分数详情列表异常", e);
			commonResp.exception("获取分数详情列表异常");
		}
		return commonResp;
	}
	
}
